package cao.oschina.function.multiple;

import android.support.v4.view.ViewPager;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import cao.oschina.R;
import cao.oschina.function.FunctionFragment;
import cao.oschina.function.multiple.blog.BlogFunc;
import cao.oschina.function.multiple.news.NewsFunc;
import cao.oschina.ui.Main;
import hd.util.adapter.pager.BasePagerAdapter;
import hd.util.widget.PagerTabView;

/**
 * 综合
 * <p/>
 * Created by Haidy on 15/4/3.
 */
public class MultipleFragment extends FunctionFragment {


    private ViewPager newsPager;
    private MultipleFunction[] functions;
    private List<View> views;

    public MultipleFragment(Main main) {
        super(main);
    }

    @Override
    protected int createView() {
        return R.layout.multiple_view;
    }

    @Override
    protected void initView(View view) {
        newsPager = (ViewPager) view.findViewById(R.id.news_pager);
        views = new ArrayList<>();
        views.add(View.inflate(app, R.layout.news_view, null));
        views.add(View.inflate(app, R.layout.news_view, null));
        views.add(View.inflate(app, R.layout.blog_view, null));
        views.add(View.inflate(app, R.layout.blog_view, null));
        newsPager.setAdapter(new BasePagerAdapter(views));
        final PagerTabView pagerTabView = (PagerTabView) view.findViewById(R.id.pagerTabView);
        newsPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            private int pageWidth = app.getScreenWidth();

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                int offsetPixels = position * pageWidth + positionOffsetPixels;
                pagerTabView.scrollBarScrollTo(-offsetPixels / views.size());
            }

            @Override
            public void onPageSelected(int position) {
                if (position == 1 && functions[position] == null) {
                    functions[position] = new NewsFunc(views.get(position), main, NewsFunc.TYPE_HOTS);
                } else if (position == 2 && functions[position] == null) {
                    functions[position] = new BlogFunc(views.get(position), main, BlogFunc.TYPE_LATEST);
                } else if (position == 3 && functions[position] == null) {
                    functions[position] = new BlogFunc(views.get(position), main, BlogFunc.TYPE_RECOMMEND);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        pagerTabView.setOnCheckedChangeListener(new PagerTabView.OnCheckedChangeListener() {
            @Override
            public void onChecked(View view) {
                int index = view.getId() - R.id.news;
                newsPager.setCurrentItem(index);
            }
        });
    }

    @Override
    protected void initData() {
        functions = new MultipleFunction[4];
        functions[0] = new NewsFunc(views.get(0), main, NewsFunc.TYPE_NEWS);
    }
}
